package com.eedi.framework.template.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eedi.framework.template.service.SysTemplatePermissionService;
import com.eedi.framework.template.controller.sys.vo.permission.SysTemplatePermissionAssignRoleMenuReq;

import javax.annotation.Resource;
import java.util.Set;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author 永聚长青源码
 */
@Tag(name = "系统界面 - 机构权限模版")
@RestController
@RequestMapping("/temp-permission")
public class SysTemplatePermissionController {

    @Resource
    private SysTemplatePermissionService sysTemplatePermissionService;


    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "sysTemplateRoleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("@sys.hasPermission('sys:temp-permission:assign-role-menu')")
    public CommonResult<Set<String>> getRoleMenuList(String sysTemplateRoleId) {
        return success(sysTemplatePermissionService.getRoleMenuListByRoleId(sysTemplateRoleId));
    }

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单")
    @PreAuthorize("@sys.hasPermission('sys:temp-permission:assign-role-menu')")
    public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody SysTemplatePermissionAssignRoleMenuReq reqVO) {

        // 执行菜单的分配
        sysTemplatePermissionService.assignRoleMenu(reqVO.getSysTemplateRoleId(), reqVO.getSysMenuIds());
        return success(true);
    }

    @PostMapping("/remove-role-menu")
    @Operation(summary = "移除角色菜单")
    @PreAuthorize("@sys.hasPermission('sys:temp-permission:assign-role-menu')")
    public CommonResult<Boolean> removeRoleMenu(@Validated @RequestBody SysTemplatePermissionAssignRoleMenuReq reqVO) {

        // 执行菜单的分配
        sysTemplatePermissionService.assignRoleMenu(reqVO.getSysTemplateRoleId(), reqVO.getSysMenuIds());
        return success(true);
    }


}
